package com.cloud.school.bus.teacherhelper.modules.settings;

import org.apache.http.Header;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.method.DigitsKeyListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;

import com.android.support.jhf.edittextclear.EditTextClearView;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.network.IErrorListener;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.base.activity.BaseActivity;
import com.cloud.school.bus.teacherhelper.base.dialog.CustomAlertDialog;
import com.cloud.school.bus.teacherhelper.base.utils.AppUserId;
import com.cloud.school.bus.teacherhelper.base.view.TitleNavBarView;
import com.cloud.school.bus.teacherhelper.modules.login.LoginActivity;
import com.cloud.school.bus.teacherhelper.protocol.accountrelevant.ModifyLoginPasswordRequest;
import com.cloud.school.bus.teacherhelper.protocol.accountrelevant.ModifyLoginPasswordResponse;

/**
 * 修改密码
 * 
 * @author lenovo
 * 
 */
public class ModifyPasswordActivity extends BaseActivity {

	private DigitsKeyListener mDigitsKeyListener = DigitsKeyListener
			.getInstance("0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
	private static final int PASSWORD_MAX = 24;
	private static final int PASSWORD_MIN = 6;

	private EditTextClearView mOldPasswordEditTextClearView;
	private EditTextClearView mNewPasswordEditTextClearView;
	private EditTextClearView mAgainPasswordEditTextClearView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_password);

		init();
	}

	@Override
	protected void init() {

		mOldPasswordEditTextClearView = (EditTextClearView) findViewById(R.id.oldPasswordEditTextClearView);
		mNewPasswordEditTextClearView = (EditTextClearView) findViewById(R.id.newPasswordEditTextClearView);
		mAgainPasswordEditTextClearView = (EditTextClearView) findViewById(R.id.againPasswordEditTextClearView);

		mOldPasswordEditTextClearView.getEditText().setKeyListener(
				mDigitsKeyListener);
		mNewPasswordEditTextClearView.getEditText().setKeyListener(
				mDigitsKeyListener);
		mAgainPasswordEditTextClearView.getEditText().setKeyListener(
				mDigitsKeyListener);

		mOldPasswordEditTextClearView.getEditText()
				.setFilters(
						new InputFilter[] { new InputFilter.LengthFilter(
								PASSWORD_MAX) });
		mNewPasswordEditTextClearView.getEditText()
				.setFilters(
						new InputFilter[] { new InputFilter.LengthFilter(
								PASSWORD_MAX) });
		mAgainPasswordEditTextClearView.getEditText()
				.setFilters(
						new InputFilter[] { new InputFilter.LengthFilter(
								PASSWORD_MAX) });

		setTitleNavBar();

		setListener();
	}

	@Override
	protected void setListener() {

	}

	protected void setTitleNavBar() {
		final TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage(getString(R.string.modify_password));
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleNavBarView.setRightButtonImageString(R.drawable.btn_confirm_xml, "",
				android.R.color.black);
		titleNavBarView.setRightButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				StringBuilder stringBuilder = new StringBuilder();
				if (checkPassword(stringBuilder)) {
					modifyLoginPassword();
				} else {
					HandlerToastUI.getHandlerToastUI(mContext,
							stringBuilder.toString());
				}
			}
		});
	}

	/**
	 * 
	 * @param stringBuilder
	 * @return
	 */
	private boolean checkPassword(StringBuilder stringBuilder) {
		stringBuilder.delete(0, stringBuilder.length());
		String oldPasswordString = mOldPasswordEditTextClearView.getEditText()
				.getText().toString();
		String newPasswordString = mNewPasswordEditTextClearView.getEditText()
				.getText().toString();
		String againPasswordString = mAgainPasswordEditTextClearView
				.getEditText().getText().toString();

		if (TextUtils.isEmpty(oldPasswordString)) {
			stringBuilder.append(getString(R.string.enter_old_password));
			return false;
		} else if (oldPasswordString.length() < PASSWORD_MIN) {
			// stringBuilder.append("旧密码大于6位");
			stringBuilder.append(getString(R.string.old_password_error));
			return false;
		} else if (TextUtils.isEmpty(newPasswordString)) {
			stringBuilder.append(getString(R.string.enter_new_password));
			return false;
		} else if (newPasswordString.length() < PASSWORD_MIN) {
			// stringBuilder.append("新密码大于6位");
			stringBuilder.append(getString(R.string.new_password_error));
			return false;
		} else if (TextUtils.isEmpty(againPasswordString)) {
			stringBuilder.append(getString(R.string.enter_new_password));
			return false;
		} else if (!againPasswordString.equals(newPasswordString)) {
			stringBuilder
					.append(getString(R.string.two_password_input_different));
			return false;
		}

		return true;
	}

	private void modifyPasswordDialog() {
		CustomAlertDialog customAlertDialog = new CustomAlertDialog(mContext,
				R.style.CustomAlertDialog);
		customAlertDialog
				.setTitleMessage(getString(R.string.tips), Color.BLACK);
		customAlertDialog
				.setMessage(getString(R.string.modify_success_please_login_agin));
		customAlertDialog.setLeftButton(getString(R.string.confirm),
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						setResult(RESULT_OK);
						finish();
					}
				});
		customAlertDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					return true;
				} else {
					return false; // 默认返回 false
				}
			}
		});
		customAlertDialog.setRightButtonGone();
		customAlertDialog.show();
	}

	private void modifyLoginPassword() {
		NetworkClient.getNetworkClient().PostRequest(
				new ModifyLoginPasswordRequest(mContext,
						mOldPasswordEditTextClearView.getEditText().getText()
								.toString(), mNewPasswordEditTextClearView
								.getEditText().getText().toString()),
				new ModifyLoginPasswordResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if ("1".equals(code)) {
							// HandlerToastUI
							// .getHandlerToastUI(mContext, "修改密码成功");
							HandlerPostUI.getHandlerPostUI(new Runnable() {

								@Override
								public void run() {
									modifyPasswordDialog();
								}
							});
						} else if ("-2".equals(code)) {
							HandlerToastUI
									.getHandlerToastUI(mContext, "输入不能为空");
						} else if ("-4".equals(code)) {
							HandlerToastUI
									.getHandlerToastUI(mContext, "新旧密码相同");
						} else if ("-31".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext,
									"老密码不能含特殊符号");
						} else if ("-32".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext,
									"新密码不能含特殊符号");
						} else if ("-1012".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext, "密码不正确");
						}
					}
				}, null, new IErrorListener() {

					@Override
					public void onFailure(int statusCode, Header[] headers,
							byte[] responseBody, Throwable error) {
						String code = "";
						for (int i = 0; i < headers.length; i++) {
							Header header = headers[i];
							if ("code".equalsIgnoreCase(header.getName())) {
								code = header.getValue();
								break;
							}
						}
						if ("1".equals(code)) {
							// HandlerToastUI
							// .getHandlerToastUI(mContext, "修改密码成功");
							HandlerPostUI.getHandlerPostUI(new Runnable() {

								@Override
								public void run() {
									modifyPasswordDialog();
								}
							});
						} else if ("-2".equals(code)) {
							HandlerToastUI
									.getHandlerToastUI(mContext, "输入不能为空");
						} else if ("-4".equals(code)) {
							HandlerToastUI
									.getHandlerToastUI(mContext, "新旧密码相同");
						} else if ("-31".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext,
									"老密码不能含特殊符号");
						} else if ("-32".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext,
									"新密码不能含特殊符号");
						} else if ("-1012".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext, "密码不正确");
						}
					}
				});
	}

}
